<!DOCTYPE html>
<html  lang="zh-CN" >
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, maximum-scale=5, viewport-fit=cover">
    <title>ZYNQ学习笔记-MISC设备驱动开发 | LO_StacNet的火柴盒</title>
    <meta name="description" content="ZYNQ学习笔记-MISC设备驱动开发MISC杂项设备，是Linux中用于归类ADC、蜂鸣器等不好分类的设备。其特点为共用一个主设备号10，不同设备使用不同设备号，同时会自动创建cdev和创建节点，大大简化字符设备的编写。">
<meta property="og:type" content="article">
<meta property="og:title" content="ZYNQ学习笔记-MISC设备驱动开发">
<meta property="og:url" content="https://lostacnet.top/post/41655/">
<meta property="og:site_name" content="LO_StacNet的火柴盒">
<meta property="og:description" content="ZYNQ学习笔记-MISC设备驱动开发MISC杂项设备，是Linux中用于归类ADC、蜂鸣器等不好分类的设备。其特点为共用一个主设备号10，不同设备使用不同设备号，同时会自动创建cdev和创建节点，大大简化字符设备的编写。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-04-16T08:55:15.000Z">
<meta property="article:modified_time" content="2024-04-16T11:31:11.037Z">
<meta property="article:author" content="LO_StacNet">
<meta property="article:tag" content="ZYNQ">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">

    
    <link rel="icon" href="/images/favicon.ico" type="image/x-icon">

    
<link rel="stylesheet" href="/css/common.min.css">



    
        <link href="//cdn.jsdelivr.net/npm/katex@0.9.0/dist/katex.min.css" rel="stylesheet">
    
    
    
    
        <link href="//cdn.jsdelivr.net/npm/lightgallery.js@1.1.3/dist/css/lightgallery.min.css" rel="stylesheet">
    
    
    
<link rel="stylesheet" href="/css/iconfont.min.css">

    
<meta name="generator" content="Hexo 6.3.0"><style>mjx-container[jax="SVG"] {
  direction: ltr;
}

mjx-container[jax="SVG"] > svg {
  overflow: visible;
}

mjx-container[jax="SVG"][display="true"] {
  display: block;
  text-align: center;
  margin: 1em 0;
}

mjx-container[jax="SVG"][justify="left"] {
  text-align: left;
}

mjx-container[jax="SVG"][justify="right"] {
  text-align: right;
}

g[data-mml-node="merror"] > g {
  fill: red;
  stroke: red;
}

g[data-mml-node="merror"] > rect[data-background] {
  fill: yellow;
  stroke: none;
}

g[data-mml-node="mtable"] > line[data-line] {
  stroke-width: 70px;
  fill: none;
}

g[data-mml-node="mtable"] > rect[data-frame] {
  stroke-width: 70px;
  fill: none;
}

g[data-mml-node="mtable"] > .mjx-dashed {
  stroke-dasharray: 140;
}

g[data-mml-node="mtable"] > .mjx-dotted {
  stroke-linecap: round;
  stroke-dasharray: 0,140;
}

g[data-mml-node="mtable"] > svg {
  overflow: visible;
}

[jax="SVG"] mjx-tool {
  display: inline-block;
  position: relative;
  width: 0;
  height: 0;
}

[jax="SVG"] mjx-tool > mjx-tip {
  position: absolute;
  top: 0;
  left: 0;
}

mjx-tool > mjx-tip {
  display: inline-block;
  padding: .2em;
  border: 1px solid #888;
  font-size: 70%;
  background-color: #F8F8F8;
  color: black;
  box-shadow: 2px 2px 5px #AAAAAA;
}

g[data-mml-node="maction"][data-toggle] {
  cursor: pointer;
}

mjx-status {
  display: block;
  position: fixed;
  left: 1em;
  bottom: 1em;
  min-width: 25%;
  padding: .2em .4em;
  border: 1px solid #888;
  font-size: 90%;
  background-color: #F8F8F8;
  color: black;
}

foreignObject[data-mjx-xml] {
  font-family: initial;
  line-height: normal;
  overflow: visible;
}

.MathJax path {
  stroke-width: 3;
}

mjx-container[display="true"] {
  overflow: auto hidden;
}

mjx-container[display="true"] + br {
  display: none;
}
</style></head>

    <body>
        <header class="header header-fixture">
    <div class="profile-search-wrap flex sm:block">
        
        
        <div class="profile sm:text-center md:px-1 lg:px-3 sm:pb-4 sm:pt-6">
            <a id="avatar" role="link" href="http://www.lostacnet.top" class="inline-block lg:w-16 lg:h-16 w-8 h-8 m-2" target="_blank" rel="noopener" rel="noreferrer" >
                <img src="/images/avatar.jpg" class="rounded-full" alt="avatar">
            </a>
            <h2 id="name" class="hidden lg:block">LO_StacNet</h2>
            <h3 id="title" class="hidden lg:block">电子玩家 &amp; 理想主义</h3>
            
            <small id="location" class="hidden lg:block">
                <i class="iconfont icon-map-icon"></i>
                Sichuan, China
            </small>
            
        </div>
        
        
<div class="search flex-1 flex lg:inline-block sm:hidden lg:px-4 lg:mt-2 lg:mb-4 lg:w-full">
    <form id="search-form" class="my-auto flex-1 lg:border lg:border-solid lg:border-gray-200">
        <div class="input-group table bg-gray-100 lg:bg-white w-full">
            <input id="search-input" type="text" placeholder="搜索" class="inline-block w-full bg-gray-100 lg:bg-white p-1">
            <span class="table-cell">
                <button name="search tigger button" disabled>
                    <i class="iconfont icon-search m-2"></i>
                </button>
            </span>
        </div>
    </form>
        
<div id="content-json" data-placeholder="搜索" class="invisible hidden">/content.json</div>
<script id="search-teamplate" type="text/html" data-path="/content.json">
    <div>
        <div class="search-header bg-gray-400">
            <input id="actual-search-input" model="keyword" ref="input" class="inline-block w-full h-10 px-2 py-1" placeholder="搜索" type="text">
        </div>
        <div class="search-result bg-gray-200">
            {{#each searchPosts}}
            <a href="/{{ path }}" class="result-item block px-2 pb-3 mb-1 pt-1 hover:bg-indigo-100">
                <i class="iconfont icon-file"></i>
                <h1 class="result-title inline font-medium text-lg">{{ title }}</h1>
                <p class="result-content text-gray-600 text-sm">{{{ text }}}</p>
            </a>
            {{/each}}
        </div>
    </div>
</script>

</div>


        <button name="menu toogle button" id="menu-toggle-btn" class="block sm:hidden p-3" role="button" aria-expanded="false">
            <i class="iconfont icon-hamburger"></i>
        </button>
    </div>
    <nav id="menu-nav" class="hidden sm:flex flex-col">
        
        
            <div class="menu-item menu-home" role="menuitem">
                <a href="/.">
                    <i class="iconfont icon-home" aria-hidden="true"></i>
                    <span class="menu-title">首页</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-archives" role="menuitem">
                <a href="/archives">
                    <i class="iconfont icon-archive" aria-hidden="true"></i>
                    <span class="menu-title">归档</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-categories" role="menuitem">
                <a href="/categories">
                    <i class="iconfont icon-folder" aria-hidden="true"></i>
                    <span class="menu-title">分类</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-tags" role="menuitem">
                <a href="/tags">
                    <i class="iconfont icon-tag" aria-hidden="true"></i>
                    <span class="menu-title">标签</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-links" role="menuitem">
                <a href="/links">
                    <i class="iconfont icon-friend" aria-hidden="true"></i>
                    <span class="menu-title">友链</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-about" role="menuitem">
                <a href="/about">
                    <i class="iconfont icon-cup" aria-hidden="true"></i>
                    <span class="menu-title">关于</span>
                </a>
            </div>
        
        
<div class="social-links flex sm:flex-col lg:hidden mt-5">
    
        <span class="social-item text-center">
            <a target="_blank" rel="noopener" href="https://github.com/LOStacNet">
                <i class="iconfont social-icon icon-github"></i>
                <span class="menu-title hidden lg:inline">menu.github</span>
            </a>
        </span>
    
        <span class="social-item text-center">
            <a target="_blank" rel="noopener" href="https://gitee.com/LOStacNet">
                <i class="iconfont social-icon icon-project"></i>
                <span class="menu-title hidden lg:inline">menu.project</span>
            </a>
        </span>
    
</div>


    </nav>
</header>

        <section class="main-section">
            
    <main class="flex-1 px-4 py-14 md:px-5 lg:px-8 lg:py-4 relative min-h-screen">
    

    <article class="content article article-archives article-type-list" itemscope="">
        <header class="article-header">
            
    
        <h1 class="article-title text-lg" itemprop="name">
            ZYNQ学习笔记-MISC设备驱动开发
        </h1>
    



            <p class="article-meta mb-3 text-xs">
                <span class="article-date">
    <i class="iconfont icon-calendar-check"></i>
	<a href="/post/41655/" class="article-date">
	  <time datetime="2024-04-16T08:55:15.000Z" itemprop="datePublished">4月 16</time>
	</a>
</span>

                
    <span class="article-category">
    <i class="iconfont icon-folder"></i>
    <a class="article-category-link" href="/categories/ZYNQ%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">ZYNQ学习笔记</a>
  </span>


                
    <span class="article-tags">
    <i class="iconfont icon-tag"></i>
    <a class="article-tag-none-link" href="/tags/Linux/" rel="tag">Linux</a>, <a class="article-tag-none-link" href="/tags/ZYNQ/" rel="tag">ZYNQ</a>
  </span>


                <span class="_partial/post-comment"><i class="icon icon-comment"></i>
                    <a href="/post/41655/#comments" class="article-comment-link">
                        评论
                    </a>
                </span>
                
    
        <span class="post-wordcount" itemprop="wordCount">字数统计: 932(字)</span>
    
    
        <span class="post-readcount" itemprop="timeRequired">阅读时长: 4(分)</span>
    


            </p>
        </header>
        <div class="marked-body article-body">
            <h1 id="ZYNQ学习笔记-MISC设备驱动开发"><a href="#ZYNQ学习笔记-MISC设备驱动开发" class="headerlink" title="ZYNQ学习笔记-MISC设备驱动开发"></a>ZYNQ学习笔记-MISC设备驱动开发</h1><p>MISC杂项设备，是Linux中用于归类ADC、蜂鸣器等不好分类的设备。其特点为共用一个主设备号10，不同设备使用不同设备号，同时会自动创建cdev和创建节点，大大简化字符设备的编写。</p>
<span id="more"></span>

<h2 id="MISC设备简介"><a href="#MISC设备简介" class="headerlink" title="MISC设备简介"></a>MISC设备简介</h2><p>MISC设备由一个miscdevice结构体描述，该结构体在<code>include/linux/miscdevice.h  </code>中定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">miscdevice</span> {</span></span><br><span class="line">	<span class="type">int</span> minor; <span class="comment">// 次设备号</span></span><br><span class="line">	<span class="type">const</span> <span class="type">char</span> *name; <span class="comment">// 设备名字</span></span><br><span class="line">	<span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">file_operations</span> *<span class="title">fops</span>;</span> <span class="comment">// 设备操作函数集合</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">list_head</span> <span class="title">list</span>;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">device</span> *<span class="title">parent</span>;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">device</span> *<span class="title">this_device</span>;</span></span><br><span class="line">	<span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">attribute_group</span> **<span class="title">groups</span>;</span></span><br><span class="line">	<span class="type">const</span> <span class="type">char</span> *nodename;</span><br><span class="line">	<span class="type">umode_t</span> mode;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>

<p>这里我们需要关注<code>minor</code>,<code>name</code>,<code>fops</code>变量。minor为从设备号，需要用户自己指定，在<code>miscdevice.h</code>中定义了一些预定的设备号，但是一般我们只需要关注一个值：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> MISC_DYNAMIC_MINOR 255</span></span><br></pre></td></tr></table></figure>

<p>将minor设置为这个值，系统将会将动态分配设备号。</p>
<p>name就是模块名字，注册设备后将会在dev/下生成该名字的文件。</p>
<h2 id="MISC与传统字符驱动的不同"><a href="#MISC与传统字符驱动的不同" class="headerlink" title="MISC与传统字符驱动的不同"></a>MISC与传统字符驱动的不同</h2><p>普通字符设备进行注册需要进行以下步骤：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">alloc_chrdev_region(); <span class="comment">/* 申请设备号 */</span></span><br><span class="line">cdev_init(); <span class="comment">/* 初始化 cdev */</span></span><br><span class="line">cdev_add(); <span class="comment">/* 添加 cdev */</span></span><br><span class="line">class_create(); <span class="comment">/* 创建类 */</span></span><br><span class="line">device_create(); <span class="comment">/* 创建设备 */</span></span><br></pre></td></tr></table></figure>

<p>在MISC设备中，直接调用一个函数即可：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">misc_register</span><span class="params">(<span class="keyword">struct</span> miscdevice * misc)</span></span><br></pre></td></tr></table></figure>

<p>同样的，对于注销，字符设备需要执行以下步骤：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cdev_del(); <span class="comment">/* 删除 cdev */</span></span><br><span class="line">unregister_chrdev_region(); <span class="comment">/* 注销设备号 */</span></span><br><span class="line">device_destroy(); <span class="comment">/* 删除设备 */</span></span><br><span class="line">class_destroy(); <span class="comment">/* 删除类 */</span></span><br></pre></td></tr></table></figure>

<p>在MISC中，只需要一个函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">misc_deregister</span><span class="params">(<span class="keyword">struct</span> miscdevice *misc)</span></span><br></pre></td></tr></table></figure>

<p>可见MISC设备大大简化了字符设备的创建过程，同时还节约了设备号，不用手动创建节点。</p>
<h2 id="一个例子"><a href="#一个例子" class="headerlink" title="一个例子"></a>一个例子</h2><p>以PIR红外传感器驱动为例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/platform_device.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/miscdevice.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/ioport.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/of.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/fs.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/delay.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;asm/io.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/time.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;asm/uaccess.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 驱动名称 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEVICE_NAME <span class="string">"ccd_pir"</span></span></span><br><span class="line"><span class="comment">/* gpio 寄存器虚拟地址 */</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> __iomem *gpio_add_minor_pir;</span><br><span class="line"><span class="comment">//static void __iomem *gpio_add_minor_led;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* gpio 寄存器物理基地址 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIO_BASE_PIR 0x41210000</span></span><br><span class="line"><span class="comment">//#define GPIO_BASE_LED 0x41200000</span></span><br><span class="line"></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> GPIO_TRI_Offset = <span class="number">0x4</span>;</span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> GPIO_DATA_Offset = <span class="number">0x0</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">volatile</span> <span class="type">long</span> pir[<span class="number">1</span>] = {<span class="number">0</span>};</span><br><span class="line"><span class="type">void</span> <span class="title function_">PIR</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"><span class="type">int</span> label_pir, state_pir;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*打开函数，当打开驱动设备节点时，执行此函数,相当于初始化设备文件*/</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">pir_open</span><span class="params">(<span class="keyword">struct</span> inode *inode,<span class="keyword">struct</span> file *filp)</span></span><br><span class="line">{</span><br><span class="line">	gpio_add_minor_pir = (<span class="type">unsigned</span> <span class="type">int</span>)ioremap(GPIO_BASE_PIR, <span class="number">32</span>);</span><br><span class="line">	<span class="comment">//gpio_add_minor_led = (unsigned int)ioremap(GPIO_BASE_LED, 32);	</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/* write 函数实现, 对应到 Linux 系统调用函数的 write 函数 */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">pir_write</span><span class="params">(<span class="keyword">struct</span> file *file_p, <span class="type">const</span> <span class="type">char</span> __user *buf, <span class="type">size_t</span> len, <span class="type">loff_t</span> *loff_t_p)</span></span><br><span class="line">{</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/* release 函数实现, 对应到 Linux 系统调用函数的 close 函数 */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">pir_release</span><span class="params">(<span class="keyword">struct</span> inode *inode_p, <span class="keyword">struct</span> file *file_p)</span></span><br><span class="line">{</span><br><span class="line">	iounmap((<span class="type">unsigned</span> <span class="type">int</span> *)gpio_add_minor_pir);</span><br><span class="line">	<span class="comment">//iounmap((unsigned int *)gpio_add_minor_led);</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 控 制 函 数 ， 相 当 于 给 ult 发 送 控 制 命 令*/</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">pir_ioctl</span><span class="params">(<span class="keyword">struct</span> file *file, <span class="type">unsigned</span> <span class="type">int</span> cmd, <span class="type">unsigned</span> <span class="type">long</span> arg)</span></span><br><span class="line">{</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	if(arg == 0)</span></span><br><span class="line"><span class="comment">	{</span></span><br><span class="line"><span class="comment">		int state = 0;</span></span><br><span class="line"><span class="comment">		if(ioread32(gpio_add_minor_pir))</span></span><br><span class="line"><span class="comment">		{</span></span><br><span class="line"><span class="comment">			mdelay(2);</span></span><br><span class="line"><span class="comment">			if(ioread32(gpio_add_minor_pir))</span></span><br><span class="line"><span class="comment">				state =1;</span></span><br><span class="line"><span class="comment">		}</span></span><br><span class="line"><span class="comment">		if(state)</span></span><br><span class="line"><span class="comment">		{</span></span><br><span class="line"><span class="comment">			iowrite32(4, gpio_add_minor_led);</span></span><br><span class="line"><span class="comment">			mdelay(1000);</span></span><br><span class="line"><span class="comment">			iowrite32(0, gpio_add_minor_led);</span></span><br><span class="line"><span class="comment">		}</span></span><br><span class="line"><span class="comment">	}	</span></span><br><span class="line"><span class="comment">	else </span></span><br><span class="line"><span class="comment">		iowrite32(2, gpio_add_minor_led);</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 读函数*/</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">pir_read</span><span class="params">(<span class="keyword">struct</span> file *filp, <span class="type">char</span> __user *buff, <span class="type">size_t</span> count, <span class="type">loff_t</span> *offp)</span></span><br><span class="line">{</span><br><span class="line">	pir[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">	PIR();</span><br><span class="line">	pir[<span class="number">0</span>] = state_pir;</span><br><span class="line"></span><br><span class="line">	printk(KERN_DEBUG<span class="string">"state_pir: %d\n"</span>, state_pir);</span><br><span class="line"></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> err;</span><br><span class="line">	err = copy_to_user(buff, (<span class="type">const</span> <span class="type">void</span> *)pir, min(<span class="keyword">sizeof</span>(pir), count));</span><br><span class="line">	<span class="keyword">return</span> err ? -EFAULT : min(<span class="keyword">sizeof</span>(pir), count);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/*struct file_operations 结构体 , 指 向 对 应 的 驱 动 操 作 函 数*/</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">file_operations</span> <span class="title">pir_fops</span>=</span></span><br><span class="line">{</span><br><span class="line"> .owner = THIS_MODULE,</span><br><span class="line"> .open = pir_open,</span><br><span class="line"> .unlocked_ioctl = pir_ioctl,</span><br><span class="line"> .read = pir_read,</span><br><span class="line"> .write = pir_write,</span><br><span class="line"> .release = pir_release,</span><br><span class="line">};</span><br><span class="line"><span class="comment">/*注册模块*/</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">miscdevice</span> <span class="title">misc</span> =</span> </span><br><span class="line">{</span><br><span class="line">.minor = MISC_DYNAMIC_MINOR,</span><br><span class="line">.name = DEVICE_NAME,</span><br><span class="line">.fops = &amp;pir_fops,</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="comment">/*led 驱 动 加 载函数*/</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> __init <span class="title function_">pir_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> ret;</span><br><span class="line">ret = misc_register(&amp;misc);<span class="comment">//注册设备， 混合设备驱动类，此时不需要手动分配主从设备号</span></span><br><span class="line">printk (DEVICE_NAME<span class="string">"\tinitialized\n"</span>);</span><br><span class="line"><span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*led 驱 动 卸 载 函 数*/</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> __exit <span class="title function_">pir_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line">misc_deregister(&amp;misc);<span class="comment">//卸载设备</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">PIR</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line">	<span class="comment">//label_pir = 0;</span></span><br><span class="line">	state_pir = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span>(ioread32(gpio_add_minor_pir))</span><br><span class="line">	{</span><br><span class="line">		mdelay(<span class="number">2</span>);</span><br><span class="line">		<span class="keyword">if</span>(ioread32(gpio_add_minor_pir))</span><br><span class="line">			state_pir = <span class="number">1</span>;		</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">module_init(pir_init);</span><br><span class="line">module_exit(pir_exit);</span><br><span class="line"></span><br><span class="line">MODULE_LICENSE(<span class="string">"GPL"</span>);</span><br><span class="line">MODULE_AUTHOR(<span class="string">"ccd"</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>


        </div>
        
<blockquote class="copyright">
    <p><strong>本文链接 : </strong><a class="permalink" href="https://lostacnet.top/post/41655/">https://lostacnet.top/post/41655/</a></p>
    <p><strong>This article is available under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener noreferrer">Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a> License  转载请注明出处</strong></p>
</blockquote>


    </article>
    
    <section id="comments">
        
    </section>


    

</main>


<aside style="" id="sidebar" class="aside aside-fixture">
    <div class="toc-sidebar">
        <nav id="toc" class="article-toc">
            <h3 class="toc-title">文章目录</h3>
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#ZYNQ%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-MISC%E8%AE%BE%E5%A4%87%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91"><span class="toc-number">1.</span> <span class="toc-text">ZYNQ学习笔记-MISC设备驱动开发</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#MISC%E8%AE%BE%E5%A4%87%E7%AE%80%E4%BB%8B"><span class="toc-number">1.1.</span> <span class="toc-text">MISC设备简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MISC%E4%B8%8E%E4%BC%A0%E7%BB%9F%E5%AD%97%E7%AC%A6%E9%A9%B1%E5%8A%A8%E7%9A%84%E4%B8%8D%E5%90%8C"><span class="toc-number">1.2.</span> <span class="toc-text">MISC与传统字符驱动的不同</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90"><span class="toc-number">1.3.</span> <span class="toc-text">一个例子</span></a></li></ol></li></ol>
        </nav>
    </div>
</aside>





        </section>
        <footer class="hidden lg:block fixed bottom-0 left-0 sm:w-1/12 lg:w-1/6 bg-gray-100 z-40">
    
    <div class="footer-social-links">
        
            <a target="_blank" rel="noopener" href="https://github.com/LOStacNet">
                <i class="iconfont icon-github"></i>
            </a>
        
            <a target="_blank" rel="noopener" href="https://gitee.com/LOStacNet">
                <i class="iconfont icon-project"></i>
            </a>
        
    </div>
    
    
</footer>

        <div id="mask" class="hidden mask fixed inset-0 bg-gray-900 opacity-75 z-40"></div>
        <div id="search-view-container" class="hidden shadow-xl"></div>
        
<script src="/js/dom-event.min.js"></script>



<script src="/js/local-search.min.js"></script>



    <script src="//cdn.jsdelivr.net/npm/lightgallery.js@1.1.3/dist/js/lightgallery.min.js"></script>
    
<script src="/js/light-gallery.min.js"></script>






    </body>
</html>
